package com.controller.login;

import com.afbx.common.convert.Convert;
import com.entity.Admin;
import com.service.ICaptchaService;
import com.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 登录控制层
 *
 * @Author ri.yang
 * @Since 2016/1/6.
 */
@RequestMapping("/login")
@Controller
public class LoginController {
    @Resource
    private ICaptchaService captchaService;
    @Resource
    private IUserService userService;

    /**
     * 登录首页
     *
     * @return
     */
    @RequestMapping("/toIndex")
    public String toIndex() {
        return "login";
    }

    /**
     * 用户登录操作
     *
     * @param model
     * @param username
     * @param password
     * @param memberType
     * @return
     */
    @RequestMapping("/toLogin")
    public String toLogin(Model model,String username,String password,String memberType,HttpServletRequest request){
        Admin admin=userService.isValid(username, password);
        //存入session
        request.getSession().setAttribute("admin",admin);
        model.addAttribute("admin",admin);
        if (memberType!=null) {
            model.addAttribute("memberType", Convert.asInt(memberType));
        }
        return "common/main";
    }
}
